﻿using System;
using System.Collections.Generic;
using System.Text;
using BL;
using System.Data;
using Database;


namespace DB
{
    public class NagruzkaMapper:IMapper
    {
        List<Entity> nagruzkaList;

        #region IMapper Members

        public List<Entity> LoadAll()
        {
            SubjectItemMapper sMapper = new SubjectItemMapper();
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            nagruzkaList = new List<Entity>();
            foreach (DataRow dr in dt.Rows)
            {
                NagruzkaItem nagruzka = new NagruzkaItem();
                try
                {
                    nagruzka.ID = Convert.ToInt64(dr["id"]);
                    nagruzka.Chislo = (string)dr["chislo"];
                    nagruzka.Theme = (string)dr["theme"];
                    nagruzka.SubjectItem = (SubjectItem)sMapper.Find(long.Parse(dr["subject_id"].ToString()));
                    nagruzka.Hour = (int)dr["chasi"];
                }
                catch
                {
                    continue;
                }
                nagruzkaList.Add(nagruzka);
            }
            return nagruzkaList;
        }

        public Entity Find(long id)
        {
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == id)
                {
                    string str1 = (string)dr["chislo"];
                    string str2 = (string)dr["theme"];
                   
                    SubjectItemMapper mapper = new SubjectItemMapper();
                    SubjectItem si = (SubjectItem)mapper.Find(long.Parse(dr["subject_id"].ToString()));
                    int hours = int.Parse(dr["chasi"].ToString());
                    NagruzkaItem nagruzka = new NagruzkaItem(str2, str1, si
                        , hours);
                    nagruzka.ID = Convert.ToInt64(dr["id"]);
                    return (Entity)nagruzka;
                }
            }
            return null;
        }

        public void Insert(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            NagruzkaItem nagruzka = (NagruzkaItem)e;
            DataRow dr = dt.NewRow();
            dr["id"] = nagruzka.ID;
            dr["chislo"] = nagruzka.Chislo;
            dr["theme"] = nagruzka.Theme;
            dr["subject_id"] = nagruzka.SubjectItem.ID;
            dr["chasi"] = nagruzka.Hour;
            dt.Rows.Add(dr);
        }

        public void DeleteEntity(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == e.ID)
                {
                    dr.Delete();
                    return;
                }
            }
        }

        public void UpdateEntity(Entity e)
        {
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            NagruzkaItem nagruzka = (NagruzkaItem)e;
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt64(dr["id"]) == e.ID)
                {                   
                    dr["chislo"] = nagruzka.Chislo;
                    dr["theme"] = nagruzka.Theme;
                    dr["subject_id"] = nagruzka.SubjectItem.ID;
                    dr["chasi"] = nagruzka.Hour;
                }
            }
        }

        public void ClearAll()
        {
            DataTable dt = AccessDb.Instance.GetTable("Nagruzka");
            foreach (DataRow dr in dt.Rows)
                dr.Delete();
        }

        #endregion
    }
}
